package com.faceDemo.activity;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.Log;
import android.util.Size;

import com.ai.libface.ObjLiveFace;
import com.faceDemo.R;
import com.faceDemo.camera.CameraEngine;
import com.faceDemo.currencyview.OverlayView;
import com.faceDemo.encoder.EncoderBus;


public class ClassifierActivity extends CameraActivity {

    private static final String TAG = "ClassifierActivity";

    private OverlayView trackingOverlay;

    @Override
    protected int getLayoutId() {
        return R.layout.camera_connection_fragment;
    }

    @Override
    protected Size getDesiredPreviewFrameSize() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new Size(1280, 960);
        }
        return null;
    }

    public void Registe() {

    }

    @Override
    public void onPreviewSizeChosen(final Size size) {
        Registe();
        EncoderBus.GetInstance().onSetFrameConfiguration(previewHeight, previewWidth);

        trackingOverlay = (OverlayView) findViewById(R.id.facing_overlay);
        trackingOverlay.addCallback(new OverlayView.DrawCallback() {
            @Override
            public void drawCallback(final Canvas canvas) {
                EncoderBus.GetInstance().onDraw(canvas);
                final Paint pt = new Paint();
                pt.setColor(Color.RED);
                pt.setTextSize(48f);
                int action=ObjLiveFace.INSTANCE.checkAction();
                String text="";
                int x=150,y=150;
                switch (action){
                    case 1:
                        text="点头";
                        canvas.drawText(text,x,y,pt);
                        break;
                    case 2:
                        text="摇头";
                        canvas.drawText(text,x,y,pt);
                        break;
                    case 3:
                        text="眨眼";
                        canvas.drawText(text,x,y,pt);
                        break;
                }

            }
        });
    }

    @Override
    protected void processImage() {
        if (sensorEventUtil!= null) {
            getCameraBytes();
            int degree = CameraEngine.getInstance().getCameraOrientation(sensorEventUtil.orientation);
            /**
             * 设置旋转角
             */



            Log.e("h2_00003", "degree:" + degree+"  ScreenWidth:"+CameraActivity.ScreenWidth+"  ScreenHeight:"+ CameraActivity.ScreenHeight);

            ObjLiveFace.INSTANCE.setRotation(degree - 90, false, (int) CameraActivity.ScreenWidth, (int) CameraActivity.ScreenHeight);
            int ii=ObjLiveFace.INSTANCE.processImage(mNV21Bytes);

        }

        runInBackground(new Runnable() {
            @Override
            public void run() {
                readyForNextImage();
                if (trackingOverlay!=null) {
                    trackingOverlay.postInvalidate();
                }
            }
        });
    }
}